package com.google.sydym6.view.adapters;

import java.util.List;

import com.google.sydym6.R;
import com.google.sydym6.controllers.ISongListController;
import com.google.sydym6.logic.database.data.ISongData;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.TextView;

public class SongListAdapter extends BaseAdapter {

	private Context context;
	private LayoutInflater inflater;
	private List<ISongData> songs;
	private int idItem;
	private ISongListController controller;
	
	View currentListItem;
	
	public SongListAdapter(Context context, int idItem, List<ISongData> songs, ISongListController controller) {
		this.context = context;
		this.idItem = idItem;
		this.songs = songs;
		this.controller = controller;
		
		Log.v("Lists", "History adapter created");

		inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}
	
	@Override
	public int getCount() {
		return songs.size();
	}

	@Override
	public Object getItem(int position) {
		return songs.get(position);
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		View view = convertView;
		if (view == null) {
			view = inflater.inflate(idItem, parent, false);
		}
		
		ImageButton playPauseButton = (ImageButton) view.findViewById(R.id.songPlayPauseButton);
		playPauseButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Log.v("playPauseButton", "songPlayPauseButton was clicked");
				controller.playPauseSong((ISongData) getItem(position));
			}
		});
		
		ISongData data = getSongData(position);
		((TextView) view.findViewById(R.id.songListItemArtistTitle)).setText(data.getArtist() + " - " + data.getTitle());
		((TextView) view.findViewById(R.id.songListItemDate)).setText(data.getDate());
		return view;
	}

	private ISongData getSongData(int position) {
		return ((ISongData) getItem(position));
	}

}
